/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.boundary;

import giuliani_mapelli_marocco.business.UserManager;
import giuliani_mapelli_marocco.business.TimeTools;
import giuliani_mapelli_marocco.business.WeatherNotificationManager;
import giuliani_mapelli_marocco.entity.Weathernotification;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.faces.view.ViewScoped;

@Named
@ViewScoped
public class WeatherNotificationBean implements Serializable{
    
    @EJB
    UserManager um;
    
    @EJB
    WeatherNotificationManager wnm;

    private List<Weathernotification> weatherNotificationList;
    
    /**
     * Creates a new instance of WeatherNotificationBean
     */
    public WeatherNotificationBean() {
    }
    
    /**
     * Initializes the weather notifications panel, adding all the notifications that the user have received
     */
    @PostConstruct
    public void init() {
        weatherNotificationList = wnm.getAllUserWeatherNotifications(um.getLoggedUser());
    }

    public List<Weathernotification> getWeatherNotificationList() {
        return weatherNotificationList;
    }

    public void setWeatherNotificationList(List<Weathernotification> weatherNotificationList) {
        this.weatherNotificationList = weatherNotificationList;
    }
    
    /**
     * Deletes the weather notification
     * @param n the weather notification to delete
     */
    public void deleteWeatherNotification(Weathernotification n) {
        if(n == null)
            return;
        Long id = n.getId();
        weatherNotificationList.remove(n);
        wnm.deleteWeatherNotification(id);
    }
    
    /**
     * gets the message of the notification
     * @param n notification
     * @return message
     */
    public String getMessage(Weathernotification n) {
        String message;
        switch(n.getType()) {
            case ORGANIZER_NOTIFICATION:
                Date suggestedDate = n.getSuggestedDate();
                String suggestedDateString = null;
                if(suggestedDate != null) {
                    suggestedDateString = " You can find better weather conditions since " + TimeTools.convertDateToString(suggestedDate);
                }
                message = "The weather will be bad!" + suggestedDateString;
                break;
            case PARTICIPANT_NOTIFICATION:
                message = "The weather has become bad! :(";
                break;
            case WHEATHER_CHANGED:
                message = "The weather has changed!";
                break;
            default:
                message = null;
        }
        return message;
   }
    
}
